package be.pluyckx.batteryprogress.components.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import be.pluyckx.batteryprogress.R;

/**
 * Created by Philip on 22/08/13.
 */
public class TitleBoxLayout extends LinearLayout {
  private RelativeLayout mRootExpandable = null;
  private TextView mTitle;
  private ImageView mExpand;
  private View[] mChildren;
  private boolean mExpanded = true;
  private boolean mIsExpandable = false;

  public TitleBoxLayout(Context context) {
    super(context);

    init(null);
  }

  public TitleBoxLayout(Context context, AttributeSet attrs) {
    super(context, attrs);

    init(attrs);
  }

  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  public TitleBoxLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    init(attrs);
  }

  private void init(AttributeSet attrs) {
    CharSequence title = "";
    float titlePaddingBottom = 0;
    float titleMarginBottom = 0;
    float titlePaddingTop = 0;
    float titleMarginTop = 0;
    float titlePaddingLeft = 0;
    float titleMarginLeft = 0;
    float titlePaddingRight = 0;
    float titleMarginRight = 0;
    int textSize = 0;
    int textAppearance = -1;
    Drawable titleBackground = null;
    int titleBackgroundColor = 0;
    Drawable expandBackground = null;

    if (attrs != null) {
      TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.TitleBoxLayout, 0, 0);

      try {
        mIsExpandable = a.getBoolean(R.styleable.TitleBoxLayout_expandable, false);
        title = a.getText(R.styleable.TitleBoxLayout_title);
        titleMarginLeft = a.getDimension(R.styleable.TitleBoxLayout_titleMarginLeft, 0.0f);
        titleMarginTop = a.getDimension(R.styleable.TitleBoxLayout_titleMarginTop, 0.0f);
        titleMarginRight = a.getDimension(R.styleable.TitleBoxLayout_titleMarginRight, 0.0f);
        titleMarginBottom = a.getDimension(R.styleable.TitleBoxLayout_titleMarginBottom, 0.0f);
        titlePaddingLeft = a.getDimension(R.styleable.TitleBoxLayout_titlePaddingLeft, 0.0f);
        titlePaddingTop = a.getDimension(R.styleable.TitleBoxLayout_titlePaddingTop, 0.0f);
        titlePaddingRight = a.getDimension(R.styleable.TitleBoxLayout_titlePaddingRight, 0.0f);
        titlePaddingBottom = a.getDimension(R.styleable.TitleBoxLayout_titlePaddingBottom, 0.0f);
        textSize = a.getDimensionPixelSize(R.styleable.TitleBoxLayout_titleTextSize, 0);
        textAppearance = a.getResourceId(R.styleable.TitleBoxLayout_titleTextAppearance, -1);
        titleBackground = a.getDrawable(R.styleable.TitleBoxLayout_titleBackground);
        titleBackgroundColor = a.getColor(R.styleable.TitleBoxLayout_titleBackgroundColor, Color.TRANSPARENT);
        if (mIsExpandable) {
          expandBackground = a.getDrawable(R.styleable.TitleBoxLayout_expandButtonBackground);

          if (expandBackground == null) {
            expandBackground = getResources().getDrawable(R.drawable.btn_expand);
          }
        }
      } finally {
        a.recycle();
      }
    }

    if (mTitle == null) {
      mTitle = new TextView(getContext());
    }

    if (textAppearance != -1) {
      mTitle.setTextAppearance(getContext(), textAppearance);
    } else if (textSize != 0) {
      mTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
    }

    setTitle(title);

    if (!mIsExpandable) {
      if (titleBackground == null) {
        mTitle.setBackgroundColor(titleBackgroundColor);
      } else {
        mTitle.setBackgroundDrawable(titleBackground);
      }

      LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
      addView(mTitle, params);
    } else {
      mRootExpandable = new RelativeLayout(getContext());
      LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
      addView(mRootExpandable, params);
      if (titleBackground == null) {
        mRootExpandable.setBackgroundColor(titleBackgroundColor);
      } else {
        mRootExpandable.setBackgroundDrawable(titleBackground);
      }

      mExpand = new ImageView(getContext());
      mExpand.setImageDrawable(expandBackground);

      mExpand.setId(R.id.imagebutton_expand);
      mTitle.setId(R.id.textview_title);

      RelativeLayout.LayoutParams relParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
      relParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
      relParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
      relParams.addRule(RelativeLayout.LEFT_OF, mExpand.getId());
      mRootExpandable.addView(mTitle, relParams);

      relParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
      relParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
      relParams.addRule(RelativeLayout.CENTER_VERTICAL);
      mRootExpandable.addView(mExpand, relParams);

      mRootExpandable.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
          mExpand.setSelected(!mExpand.isSelected());

          updateLayout();
        }
      });

      mExpanded = mExpand.isSelected();
    }

    MarginLayoutParams params = (MarginLayoutParams) mTitle.getLayoutParams();
    params.setMargins((int) titleMarginLeft, (int) titleMarginTop, (int) titleMarginRight, (int) titleMarginBottom);
    mTitle.setPadding((int) titlePaddingLeft, (int) titlePaddingTop, (int) titlePaddingRight, (int) titlePaddingBottom);
  }

  public void expand() {
    if (mIsExpandable) {
      if (!mExpanded) {
        mRootExpandable.performClick();
      }
    }
  }

  public void collapse() {
    if (mIsExpandable) {
      if (mExpanded) {
        mRootExpandable.performClick();
      }
    }
  }

  public void setTitle(CharSequence title) {
    if (title != null) {
      mTitle.setText(title);
    } else if (isInEditMode()) {
      mTitle.setText("Title");
    } else {
      mTitle.setText("");
    }
  }

  @Override
  protected void onFinishInflate() {
    super.onFinishInflate();

    if (mIsExpandable) {
      mChildren = new View[getChildCount() - 1];
      for (int i = 1; i < getChildCount(); i++) {
        mChildren[i - 1] = getChildAt(i);
      }

      updateLayout();
    }
  }

  private void updateLayout() {
    if (mIsExpandable) {
      if (mExpand.isSelected()) {
        if (!mExpanded) {
          for (View v : mChildren) {
            v.setVisibility(View.VISIBLE);
          }

          mExpanded = true;
        }
      } else {
        for (View v : mChildren) {
          v.setVisibility(View.GONE);
        }

        mExpanded = false;
      }
    }
  }
}
